﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class WebSite_m_data_HeightWeight : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["UserId"] == null || Session["UserIdentity"].ToString() != "2")
        {
            Response.Redirect("Default.aspx", false);
            HttpContext.Current.ApplicationInstance.CompleteRequest();
            return;
        }
        return;
    }
    /// <summary>
    /// 插入表中信息
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void InsertBtn_Click(object sender, EventArgs e)
    {
        try
        {
            string HeightHigh, HeightLow, WeightHigh, WeightLow, Sex, Level, Mark;
            HeightHigh = HeightHighTextBox.Text;
            HeightLow = HeightLowTextBox.Text;
            WeightHigh = WeightHighTextBox.Text;
            WeightLow = WeightLowTextBox.Text;
            Sex = SexDropDownList.SelectedItem.Value;
            Level = LevelTextBox.Text;
            Mark = MarkTextBox.Text;

            SqlConnection conn = new SqlConnection("Data Source=Localhost;Integrated Security=SSPI;Initial Catalog=CSHDB");
            conn.Open();
            SqlCommand command = new SqlCommand();
            command.Connection = conn;
            command.CommandText = "InsertHeightWeightLevel";
            command.CommandType = CommandType.StoredProcedure;
            SqlParameter[] ArrayParams = {
                                   new SqlParameter("@LowHeight",float.Parse(HeightLow)),
                                   new SqlParameter("@HighHeight",float.Parse(HeightHigh)),
                                   new SqlParameter("@LowWeight",float.Parse(WeightLow)),
                                   new SqlParameter("@HighWeight",float.Parse(WeightHigh)),
                                   new SqlParameter("@Sex",int.Parse(Sex)),
                                   new SqlParameter("@Mark",int.Parse(Mark)),
                                   new SqlParameter("@Level",Level)
                               };
            foreach (SqlParameter param in ArrayParams)
            {
                command.Parameters.Add(param);
            }
            if (command.ExecuteNonQuery() == 1)
            {
                WarnLabel.Text = "成功添加数据！";
                WarnLabel.Visible = true;
            }
        }
        catch(Exception ex)
        {
            //跳转到错误处理页面。
            Session["ErrorMessage"] = ex.Message + "\n Source:" + ex.Source + "\n TargetSite: " + ex.TargetSite.ToString() + "\nStackTrace:" + ex.StackTrace;
            Session["AtPage"] = "m_data_HeightWeight.aspx";
            Response.Redirect("m_error.aspx", false);
            HttpContext.Current.ApplicationInstance.CompleteRequest();
            return;

        }

    }
}
